package com.taofuxn.yycreazy.ui.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.taofuxn.yycreazy.R;
import com.taofuxn.yycreazy.adpater.VchatAdapter;
import com.taofuxn.yycreazy.mvp.BasePresenter;
import com.taofuxn.yycreazy.mvp.model.VchatBean;
import com.taofuxn.yycreazy.mvp.presenter.VchatPresenter;
import com.taofuxn.yycreazy.mvp.view.IVchatView;
import com.taofuxn.yycreazy.ports.ITaofuAdapterListener;
import com.taofuxn.yycreazy.ui.base.MvpToobarActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

/**
 * Created by taofuxn on 2016/9/24.
 */
public class VChatActivity extends MvpToobarActivity implements IVchatView, SwipeRefreshLayout.OnRefreshListener, ITaofuAdapterListener {

    @InjectView(R.id.recyclerview)
    RecyclerView recyclerview;
    @InjectView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private VchatAdapter mVchatAdapter;
    private ArrayList<VchatBean.Result.VchatInfo> mDatas;
    private VchatPresenter mVchatPresenter;

    private int curpage = 1;

    @Override
    protected void todoBeforeFinish() {
        finish();
    }

    @Override
    protected BasePresenter createDefaultPresenter() {
        mVchatPresenter = new VchatPresenter(this);
        aotuReflash();
        return mVchatPresenter;
    }

    @Override
    protected int setContentLayout() {
        return R.layout.activity_vchat;
    }

    @Override
    protected void init() {
        mDatas = new ArrayList<>();
        swipeRefreshLayout.setOnRefreshListener(this);
        mVchatAdapter = new VchatAdapter(R.layout.item_vchat,mDatas);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(mVchatAdapter);
        mVchatAdapter.setAdapterListener(this);
    }

    public void aotuReflash(){
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });
        onRefresh();
    }

    @Override
    public void dismissLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void requestSuccess(List<VchatBean.Result.VchatInfo> list) {
        mVchatAdapter.setData((ArrayList<VchatBean.Result.VchatInfo>) list);
    }

    @Override
    public void loadmoreSuccess(List<VchatBean.Result.VchatInfo> list) {
        mVchatAdapter.addData(list);
    }

    /**
     * 没有更多的数据
     */
    @Override
    public void noMoreData() {
        
    }

    @Override
    public void onRefresh() {
        curpage = 1;
        mVchatPresenter.getVchatList(curpage,true);
    }

    @Override
    public void loadMore() {
        Snackbar.make(swipeRefreshLayout,"正在加载...",Snackbar.LENGTH_SHORT).show();
        curpage += 1;
        mVchatPresenter.getVchatList(curpage,false);
    }
}
